package com.tuantuan.common.base;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.tuantuan.common.base.param.QueryParam;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.util.ObjectUtils;

import java.util.Map;
import java.util.function.Consumer;

/**
 * @author wangqinmin
 * @date 2018-11-08
 * @description: 仰天大笑出门去，我辈岂是蓬蒿人
 */
public abstract class BaseServiceImpl<M extends BaseMapper<T>, T> extends ServiceImpl<M, T> implements BaseService<T> {

    protected Logger logger = LoggerFactory.getLogger(getClass());

    /**
     * 单表-多条件分页模糊查询
     *
     * @param queryParam
     */
    protected QueryWrapper likeSearchMap(QueryParam queryParam) {
        Map<String, String> searchMap = queryParam.getSearchMap();
        QueryWrapper queryWrapper = new QueryWrapper();
        if (!ObjectUtils.isEmpty(searchMap)) {
            for (String key : searchMap.keySet()) {
                int count = 0;
                if (count % 2 == 0) {
                    queryWrapper.like(key, searchMap.get(key));
                } else {
                    queryWrapper.and(new Consumer() {
                        @Override
                        public void accept(Object o) {
                            queryWrapper.like(key, searchMap.get(key));
                        }
                    });
                }
                count++;
            }
        }
        return queryWrapper;
    }

}
